package com.cskaoyan.service.promote;

import com.cskaoyan.bean.MallAd;
import com.cskaoyan.mapper.MallAdMapper;
import com.cskaoyan.model.bo.promote.BasePara;
import com.cskaoyan.model.bo.promote.SimpleMallAd;
import com.cskaoyan.model.vo.promote_ad.BaseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *推广管理/广告管理模块 service实现类
 * @author：23117
 * @date: 2021/11/30
 */
@Service
public class AdvertServiceImpl implements AdvertService {
    @Autowired
    MallAdMapper mallAdmapper;

    //客户端list方法
    @Override
    public BaseData list(BasePara basePara) {
        PageHelper.startPage(basePara.getPage(),basePara.getLimit());
        List<MallAd> mallAds = mallAdmapper.selectList();
        PageInfo info = new PageInfo(mallAds);
        int total = (int) info.getTotal();

        return new BaseData(total,mallAds);
    }

    @Override
    public MallAd create(SimpleMallAd simpleMallAd) {

        MallAd mallAd = new MallAd();
        mallAd.setContent(simpleMallAd.getContent());
        mallAd.setLink(simpleMallAd.getLink());
        mallAd.setName(simpleMallAd.getName());
        mallAd.setUrl(simpleMallAd.getUrl());
        mallAd.setAddTime(new Date());

        Integer i = mallAdmapper.insertSimpleMallAd(mallAd);
        if (i==1) return mallAd;

        return null;
    }

    @Override
    public boolean update(MallAd mallAd) {

        Integer i = mallAdmapper.updateMallAd(mallAd);

        return i == 1;
    }

    @Override
    public boolean delete(MallAd mallAd) {
        mallAd.setDeleted(true);

        Integer i = mallAdmapper.deleteMallAd(mallAd);

        return i == 1;
    }


}
